//
//  LoginViewController.swift
//  LiuheCommunity
//
//  Created by 罗恒 on 2018/1/17.
//  Copyright © 2018年 luo. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController {

    @IBOutlet weak var phoneNum: UITextField!
    
    @IBOutlet weak var authCode: UITextField!
    
    @IBOutlet weak var btnGet: UIButton!
    
    @IBAction func getAction(_ sender: Any) {
        let numP = phoneNum.text
        let check = ForOC.checkTelNumber(numP)
        if check == true {
            
        }else{
            Utils.showTipMessage(tipMessage: "请输入正确的手机号码")
            return
        }
        WebService.shareInstance.getSmscode(phoneNo: numP, action: "4"){
            (isSuccess,par)in
            if isSuccess == true{
                self.isCounting = true
                self.remainingSeconds = 60
                self.authCode.becomeFirstResponder()
            }else {
                
            }
//            Utils.showAlertMessage(errorMessage: )
        }
    }
    
    @IBOutlet weak var btnLogin: UIButton!
    
    @IBAction func loginAction(_ sender: Any) {
       
        
        
        let a = phoneNum.text!.trimmingCharacters(in: CharacterSet.whitespaces)
        let b = authCode.text!.trimmingCharacters(in: CharacterSet.whitespaces)
        
        if a == "" || b == "" {
            Utils.showTipMessage(tipMessage: "请填写完整")
            return
        }
        
        let isTe = ForOC.checkTelNumber(a)
        if isTe == false {
            Utils.showTipMessage(tipMessage: "请输入正确的手机号码")
            return
        }
        
        WebService.shareInstance.doLogin(phoneNo: a, smscode: b){
            (isSuccess,par)in
            if isSuccess == true{
                ShareInfo.shareInstance.isLogin = true
                UserDefaults.standard.set(true, forKey: "isLogin")
                UserDefaults.standard.synchronize()
                self.dismiss(animated: true, completion: {
                    
                })

            }
        }
    }
    
    
    var countdownTimer:Timer?
    
    var isCounting = false{
        willSet{
            if newValue{
                countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
                btnGet.titleLabel?.textColor = UIColor.gray
            }else{
                countdownTimer?.invalidate()
                countdownTimer = nil
                
                btnGet.titleLabel?.textColor = UIColor.black
                
            }
            btnGet.isEnabled = !newValue
        }
    }
    
    var remainingSeconds = 0 {
        willSet {
            //            btnGET.setTitle("\(newValue)", for: .Normal)
            btnGet.setTitle("\(newValue)", for: .normal)
            
            
            if newValue <= 0 {
                //                btnGET.setTitle("获取验证码", forState: .Normal)
                btnGet.setTitle("获取验证码", for: .normal)
                isCounting = false
            }
        }
        
    }
    @objc func updateTime(timer: Timer){
        // 计时开始时，逐秒减少remainingSeconds的值
        remainingSeconds = remainingSeconds - 1
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

}
